package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        List<GraphInfo> graphInfoList = graphInfoMapper.selectApartmentGraphInfoList(id);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoList.add(graphVo);
        }
        apartmentDetailVo.setGraphVoList(graphVoList);

        //标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectApartmentLabelInfoList(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        //租金最小值
        LambdaQueryWrapper<RoomInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper
                .eq(RoomInfo::getApartmentId, id)
                .orderByAsc(RoomInfo::getRent).last("limit 1");
        apartmentDetailVo.setMinRent(roomInfoMapper.selectOne(apartmentInfoLambdaQueryWrapper).getRent());

        apartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted() == 1);

        return apartmentDetailVo;
    }

    @Override
    public ApartmentItemVo getApartmentItemVoByApartmentId(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        Long apartmentId = apartmentItemVo.getId();  //公寓id
        apartmentItemVo.setLabelInfoList(labelInfoMapper.selectApartmentLabelInfoList(apartmentId));
        apartmentItemVo.setGraphVoList(graphInfoMapper.selectApartmentGraphInfoList(apartmentId));
        LambdaQueryWrapper<RoomInfo> apartmentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentInfoLambdaQueryWrapper
                .eq(RoomInfo::getApartmentId, apartmentId)
                .orderByAsc(RoomInfo::getRent).last("limit 1");
        apartmentItemVo.setMinRent(roomInfoMapper.selectOne(apartmentInfoLambdaQueryWrapper).getRent());
        return apartmentItemVo;
    }
}




